package cn.shenzz.suixiang.array.leetcode_977;

import java.util.Arrays;

class Solution {
    public static void main(String[] args) {
        int[] nums = {-4,-1,0,3,10};
        System.out.println(Arrays.toString(sortedSquares(nums)));
    }
    public static int[] sortedSquares(int[] nums) {
        int n = nums.length - 1, k = n;
        int[] res = new int[n + 1];
        for (int i = 0, j = n; i <= j; ) {
            if (nums[i] * nums[i] > nums[j] * nums[j]) {
                res[k--] = nums[i] * nums[i];
                ++i;
            }
            else{
                res[k--] = nums[j] * nums[j];
                --j;
            }
        }
        return res;
    }
}